$(document).ready(function () {
 var tTable = $('#totalTable').DataTable({
   language: {
     "sProcessing": "处理中...",
     "sLengthMenu": "显示 _MENU_ 项结果",
     "sZeroRecords": "没有匹配结果",
     "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
     "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
     "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
     "sInfoPostFix": "",
     "sSearch": "搜索:",
     "sUrl": "",
     "sEmptyTable": "表中数据为空",
     "sLoadingRecords": "载入中...",
     "sInfoThousands": ",",
     "oPaginate": {
       "sFirst": "首页",
       "sPrevious": "上页",
       "sNext": "下页",
       "sLast": "末页"
     },
     "oAria": {
       "sSortAscending": ": 以升序排列此列",
       "sSortDescending": ": 以降序排列此列"
     }
   },
   processing: true,
   lengthChange: true,
   ordering: false,
   bStateSave: true,
   paging: true,
   searching: false,
   autoWidth: true,
   pagingType: "full_numbers",
   stripeClasses: ['table-row'],
   lengthMenu: [5, 10, 20, 50],
   pageLength: 5,
   deferRender: true,
   dom: 'r<t><"table-bottom"lp>',
   sAjaxSource: 'http://localhost:3030/',
   fnServerData: function (sSource, aoData, fnCallback) {
     $.ajax({
       url: sSource,
       type: 'GET',
       dataType: 'json',
       data: aoData,
       success: (res) => {
         // console.log(res)
         fnCallback({
           data: [
             {
               boys: res.data.freshmanBoys,
               girls: res.data.freshmanGirls,
               totalNum: res.data.freshmanNum
             },
             {
               boys: res.data.sophomoreBoys,
               girls: res.data.sophomoreGirls,
               totalNum: res.data.sophomoreNum
             },
             {
               boys: res.data.juniorBoys,
               girls: res.data.juniorGirls,
               totalNum: res.data.juniorNum
             },
             {
               boys: res.data.seniorBoys,
               girls: res.data.seniorGirls,
               totalNum: res.data.seniorNum
             }
           ]
         })
       },
       error: (error) => {
         alertTip({
           message: `表格数据请求失败，请检查网络连接 错误代码：${error.status}`,
           type: 'alert-danger'
         })
         fnCallback({data: []})
       }
     })
   },
   columns: [
     {
       render: function (data, type, row, meta) {
         // console.log(data, type, row, meta)
         switch (meta.row) {
           case 0:
             return '大一'
           case 1:
             return '大二'
           case 2:
             return '大三'
           case 3:
             return '大四'
           default:
             return ''
         }
       }
     },
     {data: 'boys', defaultContent: ''},
     {data: 'girls', defaultContent: ''},
     {data: 'totalNum', defaultContent: ''},
     {
       render: function (data, type, row, meta) {
         // console.log(meta)
         return `
            <div class="btn-container" style="display: flex; justify-content: space-around; align-items: center;">
                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#studentsInfoModal" data-whatever="${meta.row}">查看</button>
            </div>
          `
       }
     }
   ],
 });

 // 绑定事件
  (function () {
    let rowIndex = 0
    $('#add-grade').off('click').click((event) => {
      console.log('新增年级', rowIndex)
    })
    $('#change-grade').off('click').click((event) => {
      console.log('修改年级', rowIndex)
    })
    $('#delete-grade').off('click').click((event) => {
      console.log('删除年级', rowIndex)
    })

    // totalTable的行点击事件
    $('#totalTable tbody').on('click', 'tr',  function (event) {
      let curRow = $(event.currentTarget)
      let changeGrade = $('#change-grade')
      let deleteGrade = $('#delete-grade')
      rowIndex = curRow[0].rowIndex
      // console.log(curRow[0].rowIndex)
      curRow.toggleClass('row-selected').siblings().removeClass('row-selected')
      if ($('#totalTable tbody tr.row-selected').length === 0) {
        changeGrade.prop({ disabled: true })
        deleteGrade.prop({ disabled: true })
      } else {
        changeGrade.prop({ disabled: false })
        deleteGrade.prop({ disabled: false })
      }
    })
  }())
})
